package com.example.questionnaire.mbg.service.impl;

import com.example.questionnaire.dto.QuestionDTO;
import com.example.questionnaire.mbg.entity.Question;
import com.example.questionnaire.mbg.entity.Questionnaire;
import com.example.questionnaire.mbg.mapper.QuestionnaireMapper;
import com.example.questionnaire.mbg.service.IQuestionService;
import com.example.questionnaire.mbg.service.IQuestionnaireService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Primary
@Service
public class QuestionnaireServiceImpl implements IQuestionnaireService {

    @Resource
    private QuestionnaireMapper questionnaireMapper;

    @Resource
    private IQuestionService questionService;

    @Override
    public void update(Questionnaire questionnaire, List<QuestionDTO> questions) {
        questionnaireMapper.update(questionnaire);
        int idx = 0;
        for (QuestionDTO question : questions) {
            questionService.update(questionnaire.getId(), idx++, question);
        }
    }

    @Override
    public void alter(Questionnaire questionnaire, List<QuestionDTO> questions) {
        questionnaireMapper.update(questionnaire);
        List<Question> oldQuestions = questionService.listAll(questionnaire.getId());
        for (Question question : oldQuestions) {
            questionService.remove(question.getId());
        }
        int idx = 0;
        for (QuestionDTO question : questions) {
            questionService.create(question, idx++, questionnaire.getId());
        }
    }

    @Override
    public int copy(Questionnaire qn) {
        List<Question> questions = questionService.listAll(qn.getId());
        qn.setCreated(new Date());
        questionnaireMapper.create(qn);
        for (Question question : questions) {
            question.setQuestionnaireId(qn.getId());
            questionService.copy(question);
        }
        return qn.getId();
    }

    @Override
    public boolean exist(String encodedId) {
        return questionnaireMapper.exist(encodedId);
    }

    @Override
    public void setEncodedId(int qnid, String encodedId) {
        questionnaireMapper.setEncodedId(qnid, encodedId);
    }

    @Override
    public Questionnaire getEncoded(String encodedId) {
        return questionnaireMapper.getEncoded(encodedId);
    }

    @Override
    public void check(Date date) {
        questionnaireMapper.check(date);
    }

    @Override
    public int create(Questionnaire questionnaire, List<QuestionDTO> questions) {
        questionnaireMapper.create(questionnaire);
        int qnid = questionnaire.getId();

        int idx = 0;
        for (QuestionDTO question : questions) {
            questionService.create(question, idx++, qnid);
        }

        return qnid;
    }


    @Override
    public List<Questionnaire> list(int uid, int page, int pageSize) {
        return questionnaireMapper.list(uid, page * pageSize, pageSize);
    }

    @Override
    public List<Questionnaire> listInTrash(int uid, int page, int pageSize) {
        return questionnaireMapper.listInTrash(uid, page * pageSize, pageSize);
    }

    @Override
    public void publish(int qnid) {
        questionnaireMapper.publish(qnid, new Date());
    }

    @Override
    public void close(int qnid) {
        questionnaireMapper.close(qnid);
    }

    @Override
    public void remove(int qnid) {
        questionnaireMapper.remove(qnid);
    }

    @Override
    public void restore(int qnid) {
        questionnaireMapper.restore(qnid);
    }

    @Override
    public Questionnaire get(int qnid) {
        return questionnaireMapper.get(qnid);
    }

    @Override
    public void incReceiveCnt(int qnid) {
        questionnaireMapper.incReceiveCnt(qnid);
    }
}